#include "regsys.h"
#include "ui_regsys.h"
#include "goods.h"
#include "mainwindow.h"

#include <QInputDialog>
#include <QRegExpValidator>
#include <QMessageBox>
#include <QFile>
#include <QFileDialog>
#include <QDebug>

regSys::regSys(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::regSys)
{
    ui->setupUi(this);
    this->setWindowTitle("登录界面");

    //正则表达式规定输入字符
    QRegExp regExp("^[A-Za-z0-9]+$");
    ui->PasswordLine->setValidator(new QRegExpValidator(regExp, this));

    connect(ui->PasswordLine, &QLineEdit::returnPressed, this, &regSys::on_BtnEntrance_clicked);
}

regSys::~regSys()
{
    delete ui;
}

void regSys::on_BtnEntrance_clicked()
{
    num++;

    QString get_Account = ui->AccountLine->text();
    QString get_Password = ui->PasswordLine->text();

    QFile file("../storemanagement/json/ManagerAccount.json");
    file.open(QIODevice::ReadOnly);
    QByteArray array_read = file.readAll();
    QJsonDocument doc = QJsonDocument::fromJson(array_read);
    QJsonObject obj = doc.object();
    QJsonObject personObj = obj.value("person").toObject();








    //读取json中管理员数据
    QJsonArray OP_listArray = personObj.value("OP_list").toArray();
    //读取json中普通员工数据
    QJsonArray Staff_listArray = personObj.value("Staff_list").toArray();
    sumNum = OP_listArray.size()+Staff_listArray.size();//记录员工和管理员数量用于限制循环次数


    QFile filenew("../storemanagement/json/new.json");
    filenew.open(QIODevice::ReadOnly);
    QByteArray array_read_new = filenew.readAll();
    QJsonDocument doc_new = QJsonDocument::fromJson(array_read_new);
    QJsonObject obj_new = doc_new.object();
    QJsonArray New_listArray = obj_new.value("person").toArray();
    int a  = obj_new.value("totalcount").toInt();

    for(int i = 0 ;i<a;i++)
    {
        QJsonObject list_obj = New_listArray.at(i).toObject();
        QString Account = list_obj.value("Account").toString();
        QString password = list_obj.value("password").toString();
        qDebug() <<password;
        if(Account ==get_Account && password == get_Password )
        {
          goods * m = new goods();
          m->show();
          file.close();
          this->close();
          return;
        }
    }







    QStringList listAccount;

    for(int i = 0 ;i<OP_listArray.size();i++)
    {
        QJsonObject list_obj = OP_listArray.at(i).toObject();
        QString Account = list_obj.value("Account").toString();
        QString password = list_obj.value("password").toString();
        listAccount<<Account;
        if(Account ==get_Account && password == get_Password )
        {
          goods * m = new goods();
          m->show();
          file.close();
          this->close();
          return;
        }
    }
    //如果只是密码错误
    for(int i = 0; i<listAccount.count(); i++)
    {
        if(listAccount.at(i) == get_Account)
        {
            QMessageBox::critical(this,tr("错误提示"),tr("请检查你输入的密码是否正确"),QMessageBox::Ok);
            return;
        }
    }
    newMan();
}
//新用户注册
void regSys::newMan()
{
    QFile filenew("../storemagement/json/ManagerAccount.json");
    filenew.open(QIODevice::ReadWrite);

    QMessageBox::StandardButton choice=  QMessageBox::information(this,tr("提示"),
                               QString("你输入的账号密码有误，是否注册新账号\n "
                                       "现在正给你录入账号，默认密码none123\n"
                                       "密码请使用英文与数字混合"
                                       ),QMessageBox::No|QMessageBox::Yes);

    if(choice == QMessageBox::No)
    {
        MainWindow * m = new MainWindow();
        m->show();
        this->close();
        return;
    }
   if(choice == QMessageBox::Yes)
     {

       bool ok;
       QString text= QInputDialog::getText(this,tr("信息录入"),tr("请输入你的账户\n 默认密码none123"),
                                           QLineEdit::Normal,"none",&ok);
       //首次录入
       if(filenew.size()==0)
       {

           if(ok&& !text.isEmpty())
           {
               //对数据进行构建
               QJsonObject data_a;
               QJsonArray array_next;
               QJsonObject nextobj;
               data_a.insert("Account",text);
               data_a.insert("password","none123");
               array_next.append(data_a);
               nextobj.insert("person",array_next);
               nextobj.insert("sumcount",1);
               //将数据写入文件
               QJsonDocument doc;
               doc.setObject(nextobj);
               QByteArray data = doc.toJson();
               filenew.write(data);
               filenew.close();
           }
           else
           {
               qDebug()<<"$%^**********************************";
               QMessageBox::critical(this,tr("错误提示"),tr("error"));
           }

       }
       //文件不为空，里面有数据
       else
       {
           QByteArray array_read  = filenew.readAll();
           QJsonDocument doc = QJsonDocument::fromJson(array_read);
           filenew.close();

           //解析
           QJsonObject obj =    doc.object();
           int a  = obj.value("totalcount").toInt();
           QJsonArray person_array = obj.value("person").toArray();

           //数据写入
           QJsonObject data_a;
           data_a.insert("Account",text);
           data_a.insert("password","none123");
           for(int i =0 ;i<person_array.size();i++)
           {
              QJsonObject obj = person_array.at(i).toObject();
              array_people.append(obj);
           }
           array_people.append(data_a);

           //清空文档
           QFile filenew("../storemanagement/json/new.json");
           if (!filenew.open(QIODevice::WriteOnly | QIODevice::Text | QFile::Truncate))
           {
              QMessageBox::warning(this,tr("提示"),tr("清空失败"));
           }

           if(ok)
           {

               //解析
               QJsonObject include_obj;
               QJsonDocument docDecu;
               QByteArray data = docDecu.toJson();

               include_obj.insert("person", array_people);
               include_obj.insert("totalcount",a+1);

               docDecu.setObject(include_obj);
               //写入文件
               filenew.write(data);
               filenew.close();
           }
       }



   //如果3次以上就退出
       if(num >= 3)
       {
           num = 0;
           MainWindow * m = new MainWindow();
           m->show();
           this->close();
       }
   }

}


void regSys::on_BtnCancer_clicked()
{
    MainWindow * m = new MainWindow();
    m->show();
    this->close();
    return;
}
